package _binary_search;

/**
 * 540. 有序数组中的单一元素
 */
public class No540 {
    public int singleNonDuplicate(int[] nums) {
        int n = nums.length;
        int left = 0, right = n - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            boolean single = mid % 2 == 0;
            if (single) {
                if (mid + 1 < n && nums[mid] == nums[mid + 1]) left = mid + 1;
                else right = mid - 1;
            } else {
                if (nums[mid] == nums[mid - 1]) left = mid + 1;
                else right = mid - 1;
            }
        }
        return nums[left];
    }
}
